package tushu.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import tushu.Bean.BookRentJiLuBean;
import util.JDBCUtils;

public class rentJiLuDao {
	Connection con=null;
	PreparedStatement ps=null;
	
	public List<BookRentJiLuBean> selectByTiaoJian(BookRentJiLuBean jilu) {
		StringBuffer sql=new StringBuffer("select ");
		sql.append("name,cardId,rentPerson,rentCiShu,time,guihuantime");
		sql.append(" from b_rentjilu where 1=1 ");
		List<Object> list=selectAppendWhere(sql,jilu);
		ResultSet rs=null;
		List<BookRentJiLuBean> listbean=new ArrayList<>();
		try {
			con=JDBCUtils.getConnection();
			ps=con.prepareStatement(sql.toString());
			for (int i = 0; i < list.size(); i++) {
				ps.setObject(i+1, list.get(i));
			}
			rs=ps.executeQuery();
			while(rs.next()) {
				BookRentJiLuBean rentjilubean=new BookRentJiLuBean();
				rentjilubean.setCardId(rs.getInt("cardId"));
				rentjilubean.setName(rs.getString("name"));
				rentjilubean.setRentCiShu(rs.getInt("rentcishu"));
				rentjilubean.setRentPerson(rs.getString("rentperson"));
				rentjilubean.setTime(rs.getString("time"));
				rentjilubean.setGuiHuanTime(rs.getString("guihuantime"));
				listbean.add(rentjilubean);
			}
			return listbean;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;
		
	}
	private List<Object> selectAppendWhere(StringBuffer sql,BookRentJiLuBean rentjilu){
		List<Object> list=new ArrayList<>();
		
		String name = rentjilu.getName();
		if (name!=null &&!name.trim().isEmpty()) {
			sql.append(" and name=? ");
			list.add(name);
		}
		
		Integer id=rentjilu.getCardId();
		if (id!=0) {
			sql.append(" and cardId=? ");
			list.add(id);
		}
		
		String rentPerson=rentjilu.getRentPerson();
		if (rentPerson!=null && rentPerson.trim().isEmpty()) {
			sql.append(" and rentPerson=? ");
			list.add(rentPerson);
		}
		
		String time=rentjilu.getTime();
		if (time!=null && time.trim().isEmpty()) {
			sql.append(" and time=? ");
			list.add(time);
		}
		
		String guihuantime=rentjilu.getGuiHuanTime();
		if (guihuantime!=null && guihuantime.trim().isEmpty()) {
			sql.append(" and guihuantime=?");
			list.add(guihuantime);
		}
		return list;
		
	}
	
	
	public void insert(BookRentJiLuBean rent) {
		String sql="insert into b_rentjilu(name,cardId,rentPerson,rentCiShu,time"+
	")values(?,?,?,?,?)";
		try {
			con=JDBCUtils.getConnection();
			ps=con.prepareStatement(sql);
			ps.setString(1, rent.getName());
			ps.setInt(2, rent.getCardId());
			ps.setString(3, rent.getRentPerson());
			ps.setInt(4, rent.getRentCiShu());
			ps.setString(5, rent.getTime());
			ps.executeUpdate();
			
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public int  updateGuiHuan(BookRentJiLuBean jilu) {
		String sql="update b_rentjilu set guihuantime=? where name=?";
		try {
			con=JDBCUtils.getConnection();
			ps=con.prepareStatement(sql);
			ps.setString(1, jilu.getGuiHuanTime());
			ps.setString(2, jilu.getName());
			return ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return 0;
	}
}
